

export const baseMenu = "/Admin";

// 所有菜单数据：
const allMenus = [
    {
        id: "1",
        label: "CRUD",
        path:"/CRUD"
    },
    {
        id: "2",
        label: "pinia示例",
        path:"/piniademo"
    },
    {
        id: "3",
        label: "操作人员",
        path:"/OPerator"
    },
    {
        id: "4",
        label: "生命周期",
        children: [
            {
                id: "4-1",
                label: "选项式的",
                path:"/Lifecycle_options"
            },
            {
                id: "4-2",
                label: "组合式的",
                path:"/Lifecycle_composition"
            },
            {
                id: "4-3",
                label: "父子组件钩子调用顺序",
                path:"/Lifecycle_ParentSon"
            }
        ]
    },
    {
        id: "5",
        label: "组件传值",
        children: [
            {
                id: "5-1",
                label: "父子组件传值",
                path:"/ParentToSon"
            },
            {
                id: "5-2",
                label: "父子组件传值（事件总线）",
                path:"/Brother"
            },
            {
                id: "5-3",
                label: "事件总线案例",
                path:"/TransferValueDemo"
            },
            {
                id: "5-4",
                label: "父子案例",
                path:"/TransferValueDemo02"
            },
            {
                id: "5-5",
                label: "ref(选项式)",
                path:"/refdemo"
            },
            {
                id: "5-6",
                label: "ref(组合式)",
                path:"/refdemo02"
            },
            {
                id: "5-7",
                label: "跨级组件传值",
                path:"/providerAndInject"
            }
        ]
    },
    {
        id: "6",
        label: "路由传参",
        children: [
            {
                id: "6-1",
                label: "路由传参（params）",
                path:"/List"
            },
            {
                id: "6-2",
                label: "路由传参（query）",
                path:"/List02"
            }
        ]
    },
    {
        id: "7",
        label: "vuex",
        children: [
            {
                id: "7-1",
                label: "vuex示例(基本使用)",
                path:"/vuexdemo01"
            },
            {
                id: "7-2",
                label: "vuex示例（模块化）",
                path:"/vuexdemo02"
            }
        ]
    }
]


// 根据后端响应回来的checkedKeys，从allMenus里过滤出当前用户的菜单

export const getCurrentMenus = () => {

    // 1、获取当前用户的checkedKeys。
    // ["1","4-2","4-3"]
    const checkedKeys = JSON.parse(sessionStorage.getItem("checkedKeys"));


    const resultMenus = [];

    // 2、过滤：循环allMenus，判断当前菜单项的id是否在checkedKeys
    allMenus.forEach(item=>{
        if(!item.children){
            if(checkedKeys.includes(item.id)){
                resultMenus.push({...item});
            }
        }else{
            // 有子级
            // 1)、遍历子级，
            // 看看子级是否有id在checkedKeys中。如果有，push父级，如果没有，不push父级
            if(item.children.some(sonItem=>checkedKeys.includes(sonItem.id))){
                // 父级push了
                const parentMenu = {...item};
                resultMenus.push(parentMenu);
                // 处理子级
                parentMenu.children = parentMenu.children.filter(item=>checkedKeys.includes(item.id));
            }
        }
    })
    console.log("resultMenus",resultMenus);
    return resultMenus;
}
